#include "exitInfo.h"
#include "main.h"
lv_timer_t *ui_timer;

static lv_obj_t *label;// 通用
static lv_obj_t *obj;// 通用
static lv_obj_t *page; // 出口页面

static lv_obj_t *drop_1;// 下拉选择
static  lv_obj_t *change_print_mode_1;
static lv_obj_t *exit1_grade;// 出口等级
static lv_obj_t *fruitname1;// 水果名称
static lv_obj_t *box1_img; // 装箱图片
static lv_obj_t *boxed1_progress;// 装箱进度
static lv_obj_t *boxed1_percent; // 装箱进度百分比
static lv_obj_t *box1_cnt;//装箱个数
static lv_obj_t *exit1_outlet_percent; // 出口 出果率

static lv_obj_t *drop_2;// 下拉选择
static  lv_obj_t *change_print_mode_2;
static lv_obj_t *exit2_grade;// 出口等级
static lv_obj_t *fruitname2;// 水果名称
static lv_obj_t *box2_img; // 装箱图片
static lv_obj_t *boxed2_progress;// 装箱进度
static lv_obj_t *boxed2_percent; // 装箱进度百分比
static lv_obj_t *box2_cnt;//装箱个数
static lv_obj_t *exit2_outlet_percent; // 出口 出果率

const char *drop_text = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n" \
                        "21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n" \
                        "41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n" \
                        "61\n62\n63\n64";

static void fill_arc_cb(lv_event_t* e);
static void ui_reflush_cb(lv_timer_t *timer);
static void img_reflush_cb(lv_timer_t *timer);
static void change_print_mode_cb(lv_event_t *e);

lv_obj_t *parent = NULL;
lv_obj_t *exitInof_PageInit(lv_obj_t *parent_screen, int page_width, int page_hight)
{
    parent = parent_screen;
    lv_obj_t *page = lv_obj_create (parent_screen);
    lv_obj_clear_flag(page, LV_OBJ_FLAG_SCROLLABLE);// 禁用滚轮
    lv_obj_set_align(page, LV_ALIGN_CENTER);
    lv_obj_set_size(page, page_width, LV_SIZE_CONTENT);
    
    int width = page_width/3*2;
    int height = page_hight-25-70;
    if (system_info->exit_cnt == 2)
        width = page_width/3+30;

    lv_obj_t * work_info_1 = lv_obj_create(page);
    lv_obj_set_size(work_info_1, width, height);

    drop_1 = lv_dropdown_create(work_info_1);
    lv_obj_set_size(drop_1, width/4, LV_SIZE_CONTENT);
    lv_dropdown_set_options(drop_1, drop_text);
    lv_obj_set_style_text_font(drop_1, LV_FONT_DEFAULT, 0);
    lv_obj_set_align(drop_1, LV_ALIGN_TOP_LEFT);

    // require change printer mode
    change_print_mode_1 = lv_button_create(page);
    lv_obj_set_size(change_print_mode_1, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    label = lv_label_create (change_print_mode_1);
    lv_label_set_text (label, CHANGE_PRINT_MODE);
    lv_obj_add_event_cb (change_print_mode_1,change_print_mode_cb, LV_EVENT_CLICKED, "1");
    // lv_obj_set_align (change_print_mode, LV_ALIGN_BOTTOM_LEFT);

    exit1_grade = lv_label_create(work_info_1);
    lv_label_set_text_fmt(exit1_grade, "%s", exitGradeText);
    lv_obj_set_align(exit1_grade, LV_ALIGN_TOP_RIGHT);
    
    fruitname1 = lv_label_create(work_info_1);
    lv_obj_set_align(fruitname1, LV_ALIGN_TOP_MID); 
    lv_label_set_text(fruitname1, "fruit");

    box1_img = lv_image_create(work_info_1);
    lv_timer_create(img_reflush_cb, 500, NULL);
    lv_obj_center(box1_img);

    #if BUILD_LINUX
    width = page_width - page_width/2 - page_width/4;
    #else
    width = page_width/3*1 - 80;
    #endif
    if (system_info->exit_cnt == 2)
        width = page_width/8;
    height = (page_hight-65)/3-15;
    static lv_style_t scr1_style;
    lv_style_init(&scr1_style);
    lv_style_set_layout(&scr1_style, LV_LAYOUT_FLEX); // flex布局
    lv_style_set_flex_flow(&scr1_style, LV_FLEX_ALIGN_SPACE_AROUND);//LV_FLEX_ALIGN_SPACE_AROUND LV_FLEX_FLOW_COLUMN
    lv_obj_t * percent_scr1 = lv_obj_create(page);
    lv_obj_clear_flag(percent_scr1, LV_OBJ_FLAG_SCROLLABLE);// 禁用滚轮
    lv_obj_add_style(percent_scr1, &scr1_style, 0);

    lv_obj_set_size(percent_scr1, width, height);

    label = lv_label_create(percent_scr1);
    lv_label_set_text(label, BoxedPercent);
    lv_obj_set_align(label, LV_ALIGN_TOP_MID);

    boxed1_progress = lv_arc_create(percent_scr1);  // 创建圆弧
    lv_obj_remove_style(boxed1_progress, NULL, LV_PART_KNOB); // 移除圆弧手柄
    // lv_obj_add_flag(boxed1_progress, LV_OBJ_FLAG_CLICKABLE);  // 允许点击事件
    lv_obj_clear_flag(boxed1_progress, LV_OBJ_FLAG_CLICKABLE);  // 禁止点击事件
    lv_obj_set_size(boxed1_progress, height-50, height-50);// 设置圆弧大小
    if (system_info->exit_cnt == 2)
        lv_obj_set_size(boxed1_progress, width/3*2, width/3*2);// 设置圆弧大小
    lv_obj_set_style_arc_width(boxed1_progress, 25, LV_PART_INDICATOR); // 设置前景弧宽度
    lv_obj_set_style_arc_width(boxed1_progress, 25, LV_PART_MAIN);// 设置背景弧宽度
    lv_obj_set_style_arc_color(boxed1_progress, lv_palette_main(LV_PALETTE_GREEN), LV_PART_INDICATOR); // 设置前景弧颜色
    lv_obj_set_style_arc_color(boxed1_progress, lv_palette_main(LV_PALETTE_YELLOW), LV_PART_MAIN);// 设置背景弧颜色

    lv_obj_set_style_arc_rounded(boxed1_progress, false, LV_PART_MAIN);// 取消背景弧圆角
    lv_arc_set_angles(boxed1_progress, 0, 360); // 设置前景弧角度 旋转360度
    lv_arc_set_bg_angles(boxed1_progress, 0, 360); // 设置背景弧角度 旋转360度
    lv_arc_set_rotation(boxed1_progress, 90);// 设置起点偏移量 -> 顺时针旋转90度
    lv_arc_set_range(boxed1_progress, 0, 100); // 设置圆弧范围
    lv_arc_set_change_rate(boxed1_progress, 1000); // 设置弧的速度 值越大速度越快
    boxed1_percent = lv_label_create(boxed1_progress);
    lv_obj_center(boxed1_percent);
    lv_arc_set_value(boxed1_progress, 0); 	 // 设置圆弧当前值
    lv_label_set_text_fmt(boxed1_percent, "%u%%", 0);       // 更新label的显示
    lv_obj_add_event_cb(boxed1_progress, fill_arc_cb, LV_EVENT_VALUE_CHANGED, (void*)boxed1_percent); // 添加回调函数
    ui_timer = lv_timer_create(ui_reflush_cb, 500, NULL);

    lv_obj_t * boxed_cnt_scr1 = lv_obj_create(page);
    lv_obj_set_size(boxed_cnt_scr1, width, height);
    label = lv_label_create(boxed_cnt_scr1);
    lv_label_set_text(label, BoxedCnt);
    lv_obj_set_align(label, LV_ALIGN_TOP_MID);
    box1_cnt = lv_label_create(boxed_cnt_scr1);
    lv_label_set_text(box1_cnt, "0.0");
    lv_obj_set_align(box1_cnt, LV_ALIGN_CENTER);

    lv_obj_t * outlet_fruit_scr1 = lv_obj_create(page);
    lv_obj_set_size(outlet_fruit_scr1, width, height);
    label = lv_label_create(outlet_fruit_scr1);
    lv_label_set_text(label, FruitingEfficiency);
    lv_obj_set_align(label, LV_ALIGN_TOP_MID);
    exit1_outlet_percent = lv_label_create(outlet_fruit_scr1);
    char f[10];
    sprintf (f, "%0.1f%%", 0.0);
    lv_label_set_text(exit1_outlet_percent, f);
    lv_obj_set_align(exit1_outlet_percent, LV_ALIGN_CENTER);

    lv_obj_t * work_info_2;
    lv_obj_t * percent_scr2;
    lv_obj_t * boxed_cnt_scr2;
    lv_obj_t * outlet_fruit_scr2;
    if (system_info->exit_cnt == 2)
    {
        // work-info
        width = page_width/3+30;
        height = page_hight-25-70;

        work_info_2 = lv_obj_create(page);
        lv_obj_set_size(work_info_2, width, height);

        drop_2 = lv_dropdown_create(work_info_2);
        lv_obj_set_size(drop_2, width/4, LV_SIZE_CONTENT);
        lv_obj_set_style_text_font(drop_2, LV_FONT_DEFAULT, 0);
        lv_dropdown_set_options(drop_2, drop_text);
        lv_obj_set_align(drop_2, LV_ALIGN_TOP_LEFT);
        
        change_print_mode_2 = lv_button_create(page);
        lv_obj_set_size(change_print_mode_2, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
        label = lv_label_create (change_print_mode_2);
        lv_label_set_text (label, CHANGE_PRINT_MODE);
        lv_obj_add_event_cb (change_print_mode_2,change_print_mode_cb, LV_EVENT_CLICKED, "2");
        // lv_obj_set_align (label, LV_ALIGN_BOTTOM_RIGHT);

        exit2_grade = lv_label_create(work_info_2);
        lv_label_set_text_fmt(exit2_grade, "%s", exitGradeText);
        lv_obj_set_align(exit2_grade, LV_ALIGN_TOP_RIGHT);

        fruitname2 = lv_label_create(work_info_2);
        lv_obj_set_align(fruitname2, LV_ALIGN_TOP_MID); 
        lv_label_set_text(fruitname2, "fruit");

        box2_img = lv_image_create(work_info_2);
        lv_obj_center(box2_img);

        #if BUILD_LINUX
        width = page_width - page_width/2 - page_width/4;
        #else
        width = page_width - page_width/2 - page_width/4;
        #endif
        width = page_width/8;
        height = (page_hight-65)/3-15;
        static lv_style_t scr2_style;
        lv_style_init(&scr2_style);
        lv_style_set_layout(&scr2_style, LV_LAYOUT_FLEX); // flex布局
        lv_style_set_flex_flow(&scr2_style, LV_FLEX_ALIGN_SPACE_AROUND);//LV_FLEX_ALIGN_SPACE_AROUND LV_FLEX_FLOW_COLUMN
        percent_scr2 = lv_obj_create(page);
        lv_obj_clear_flag(percent_scr2, LV_OBJ_FLAG_SCROLLABLE);// 禁用滚轮
        lv_obj_add_style(percent_scr2, &scr2_style, 0);

        lv_obj_set_size(percent_scr2, width, height);

        label = lv_label_create(percent_scr2);
        lv_label_set_text(label, BoxedPercent);
        lv_obj_set_align(label, LV_ALIGN_TOP_MID);

        boxed2_progress = lv_arc_create(percent_scr2);  // 创建圆弧
        lv_obj_remove_style(boxed2_progress, NULL, LV_PART_KNOB); // 移除圆弧手柄
        // lv_obj_add_flag(boxed2_progress, LV_OBJ_FLAG_CLICKABLE);  // 允许点击事件
        lv_obj_clear_flag(boxed2_progress, LV_OBJ_FLAG_CLICKABLE);  // 禁止点击事件
        lv_obj_set_size(boxed2_progress, width/3*2, width/3*2);// 设置圆弧大小
        lv_obj_set_style_arc_width(boxed2_progress, 25, LV_PART_INDICATOR); // 设置前景弧宽度
        lv_obj_set_style_arc_width(boxed2_progress, 25, LV_PART_MAIN);// 设置背景弧宽度
        lv_obj_set_style_arc_color(boxed2_progress, lv_palette_main(LV_PALETTE_GREEN), LV_PART_INDICATOR); // 设置前景弧颜色
        lv_obj_set_style_arc_color(boxed2_progress, lv_palette_main(LV_PALETTE_YELLOW), LV_PART_MAIN);// 设置背景弧颜色

        lv_obj_set_style_arc_rounded(boxed2_progress, false, LV_PART_MAIN);// 取消背景弧圆角
        lv_arc_set_angles(boxed2_progress, 0, 360); // 设置前景弧角度 旋转360度
        lv_arc_set_bg_angles(boxed2_progress, 0, 360); // 设置背景弧角度 旋转360度
        lv_arc_set_rotation(boxed2_progress, 90);// 设置起点偏移量 -> 顺时针旋转90度
        lv_arc_set_range(boxed2_progress, 0, 100); // 设置圆弧范围
        lv_arc_set_change_rate(boxed2_progress, 1000); // 设置弧的速度 值越大速度越快
        boxed2_percent = lv_label_create(boxed2_progress);
        lv_obj_center(boxed2_percent);
        lv_arc_set_value(boxed2_progress, 0); 	 // 设置圆弧当前值
        lv_label_set_text_fmt(boxed2_percent, "%u%%", 0);       // 更新label的显示
        lv_obj_add_event_cb(boxed2_progress, fill_arc_cb, LV_EVENT_VALUE_CHANGED, (void*)boxed2_percent); // 添加回调函数

        boxed_cnt_scr2 = lv_obj_create(page);
        lv_obj_set_size(boxed_cnt_scr2, width, height);
        label = lv_label_create(boxed_cnt_scr2);
        lv_label_set_text(label, BoxedCnt);
        lv_obj_set_align(label, LV_ALIGN_TOP_MID);
        box2_cnt = lv_label_create(boxed_cnt_scr2);
        lv_label_set_text(box2_cnt, "0.0");
        lv_obj_set_align(box2_cnt, LV_ALIGN_CENTER);

        outlet_fruit_scr2 = lv_obj_create(page);
        lv_obj_set_size(outlet_fruit_scr2, width, height);
        label = lv_label_create(outlet_fruit_scr2);
        lv_label_set_text(label, FruitingEfficiency);
        lv_obj_set_align(label, LV_ALIGN_TOP_MID);
        exit2_outlet_percent = lv_label_create(outlet_fruit_scr2);
        lv_label_set_text(exit2_outlet_percent, f);
        lv_obj_set_align(exit2_outlet_percent, LV_ALIGN_CENTER);
    }

    // 布局
    static lv_coord_t col_dsc[] = { LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST };
    static lv_coord_t row_dsc[] = { LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, 40, LV_GRID_TEMPLATE_LAST };
    lv_obj_set_style_grid_column_dsc_array(page, col_dsc, 0);
    lv_obj_set_style_grid_row_dsc_array(page, row_dsc, 0);
    lv_obj_set_layout(page, LV_LAYOUT_GRID);

    lv_obj_set_grid_cell(work_info_1, LV_GRID_ALIGN_SPACE_AROUND, 1 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 0, 3);
    lv_obj_set_grid_cell(change_print_mode_1, LV_GRID_ALIGN_SPACE_AROUND, 1 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 3, 1);
    lv_obj_set_grid_cell(percent_scr1, LV_GRID_ALIGN_SPACE_AROUND, 2 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 0, 1);
    lv_obj_set_grid_cell(boxed_cnt_scr1, LV_GRID_ALIGN_SPACE_AROUND, 2 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 1, 1);
    lv_obj_set_grid_cell(outlet_fruit_scr1, LV_GRID_ALIGN_SPACE_AROUND, 2 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 2, 1);


    lv_obj_set_grid_cell(drop_1, LV_GRID_ALIGN_SPACE_AROUND, 0, 1,LV_GRID_ALIGN_SPACE_AROUND, 1, 1); 
    if (system_info->exit_cnt == 2)
    {
        lv_obj_set_grid_cell(work_info_2, LV_GRID_ALIGN_SPACE_AROUND, 3 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 0, 3);
        lv_obj_set_grid_cell(change_print_mode_2, LV_GRID_ALIGN_SPACE_AROUND, 4 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 3, 1);
        lv_obj_set_grid_cell(percent_scr2, LV_GRID_ALIGN_SPACE_AROUND, 4 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 0, 1);
        lv_obj_set_grid_cell(boxed_cnt_scr2, LV_GRID_ALIGN_SPACE_AROUND, 4 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 1, 1);
        lv_obj_set_grid_cell(outlet_fruit_scr2, LV_GRID_ALIGN_SPACE_AROUND, 4 ,1 , LV_GRID_ALIGN_SPACE_AROUND, 2, 1);

        lv_obj_set_grid_cell(drop_2, LV_GRID_ALIGN_SPACE_AROUND, 2, 1,LV_GRID_ALIGN_SPACE_AROUND, 1, 1); 
    }

    return page;
}
int reemoon_get_exit_id (int exit_id)
{
    char num[10];
    if (exit_id == 1)
        lv_dropdown_get_selected_str(drop_1, num, sizeof(num));
    else if (exit_id == 2)
        lv_dropdown_get_selected_str(drop_2, num, sizeof(num));
    return atoi(num);
}

// 设置水果名称
void reemoon_set_fruitname(int exit_id, const char *fruitname)
{
    if (fruitname != NULL && strlen(fruitname) > 0)
    {
        if (exit_id == 1)
            lv_label_set_text(fruitname1, fruitname);
        else if (exit_id == 2)
            lv_label_set_text(fruitname2, fruitname);
    }
}

// 设置出口等级
void reemoon_set_exit_grade(int exit_id, const char *grade)
{
    if (exit_id == 1)
        lv_label_set_text_fmt(exit1_grade, "%s\n%s",exitGradeText , grade);
    else if (exit_id == 2)
        lv_label_set_text_fmt(exit2_grade, "%s\n%s",exitGradeText , grade);
}

// 设置装箱图片
void reemoon_set_box_img (int exit_id)
{
    if (exit_id == 1)
        lv_img_set_src (box1_img, "A:/reemoon/src/img/work_1.png");//img_path);
    else if (exit_id == 2)
        lv_img_set_src (box2_img, "A:/reemoon/src/img/work_2.png");//img_path);
}

static bool img1_id;
static bool img2_id;
int reemoon_set_img_id(int id)
{
    if (id == 1) img1_id = true;
    if (id == 2) img2_id = true;
    return id;
}

// 申请更换打印模板按键事件回调
static void change_print_mode_cb(lv_event_t *e)
{
    if (e->code == LV_EVENT_CLICKED)
    {
        char msg_buffer[256];
        // 发送信息给上位机
        if (strcmp("1", e->user_data) == 0)
            snprintf (&msg_buffer[0],  sizeof(msg_buffer), "{\"%s\":\"%s\"}", RABIITMQ_SENDER_ID, CMD_CHANGE_PRINT_MODE"-1");
        // 发送信息给上位机
        else if (strcmp("2", e->user_data) == 0)
            snprintf (&msg_buffer[0],  sizeof(msg_buffer), "{\"%s\":\"%s\"}", RABIITMQ_SENDER_ID, CMD_CHANGE_PRINT_MODE"-2");

        reemoon_msg_send(msg_buffer);
    }
}

static void img_reflush_cb(lv_timer_t *timer)
{
    if (img1_id)
    {
        reemoon_set_box_img(1);
        img1_id = false;
    }
    if (img2_id)
    {
        reemoon_set_box_img(2);
        img2_id = false;
    }
}

// 设置装箱进度
void reemoon_set_boxed_percent(int exit_id, float percent)
{
    char f[10];
    sprintf (f, "%d", (int)percent);
    if (exit_id == 1)
    {
        lv_arc_set_value(boxed1_progress, (int)percent); 	 // 设置圆弧当前值
        lv_label_set_text_fmt(boxed1_percent, "%s%%",f);
    }
    else if (exit_id == 2)
    {
        lv_arc_set_value(boxed2_progress, (int)percent); 	 // 设置圆弧当前值
        lv_label_set_text_fmt(boxed2_percent, "%s%%",f);
    }
}
// 设置装箱个数
void reemoon_set_box_cnt(int exit_id, float cnt)
{
    char f[10];
    sprintf (f, "%0.1f", cnt);
    if (exit_id == 1)
        lv_label_set_text(box1_cnt, f);
    else if (exit_id == 2)
        lv_label_set_text(box2_cnt, f);

}

// 设置出口效率
void reemoon_set_outlet_percent (int exit_id, float percent)
{
    char f[10];
    sprintf (f, "%0.1f%%", percent);
    if (exit_id == 1)
        lv_label_set_text(exit1_outlet_percent, f);
    else if (exit_id == 2)
        lv_label_set_text(exit2_outlet_percent, f);
}
void reemoon_clean_exit_grade()
{
    exit1.exit_grade[0] = '\0';
    exit2.exit_grade[0] = '\0';
    // memset(exit1.exit_grade, 0x00, 512);
    // memset(exit2.exit_grade, 0x00, 512);
}

static void fill_arc_cb(lv_event_t* e)
{
    lv_obj_t* label = (lv_obj_t*)lv_event_get_user_data(e); // 获取传进来的用户数据label
    lv_obj_t* arc = lv_event_get_target(e);                 // 获取触发的控件arc
    uint16_t value = lv_arc_get_value(arc);                 // 获取arc当前的值
    lv_label_set_text_fmt(label, "%u%%", value);       // 更新label的显示

    return;
}

static void ui_reflush_cb(lv_timer_t *timer)
{
    reemoon_set_fruitname(1, exit1.fruitname);
    reemoon_set_exit_grade(1, exit1.exit_grade);
    reemoon_set_boxed_percent(1, exit1.boxed_percent);
    reemoon_set_box_cnt(1, exit1.box_cnt);
    reemoon_set_outlet_percent(1, exit1.exit_percent);
    if (system_info->exit_cnt == 2)
    {
        reemoon_set_fruitname(2, exit2.fruitname);
        reemoon_set_exit_grade(2, exit2.exit_grade);
        reemoon_set_boxed_percent(2, exit2.boxed_percent);
        reemoon_set_box_cnt(2, exit2.box_cnt);
        reemoon_set_outlet_percent(2, exit2.exit_percent);
    }
    lv_obj_send_event (StatusBar.network_status, LV_EVENT_FLUSH_START, "s");
}